package com.zhiyou.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou.dao.HouseMapper;
import com.zhiyou.dao.LesseeMapper;
import com.zhiyou.dao.RentMapper;
import com.zhiyou.entity.House;
import com.zhiyou.entity.Lessee;
import com.zhiyou.entity.Rent;
import com.zhiyou.service.RentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
@Service
public class RentServiceImpl implements RentService {
    @Autowired
    RentMapper rentMapper;
    @Autowired
    HouseMapper houseMapper;
    @Autowired
    LesseeMapper lesseeMapper;
    @Override
    public PageInfo<Rent> getAll(Integer pageNo,Integer pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        System.out.println("rentMapper = " + rentMapper);
        List<Rent> list = rentMapper.selectAll2();
        System.out.println("list = " + list);
        PageInfo<Rent> pageInfo = new PageInfo<>(list,4);
        System.out.println("pageInfo = " + pageInfo);
        return pageInfo;
    }

    @Override
    public Rent selectOneByRId(Integer rId) {
        Rent rent = rentMapper.selectOneByRId(rId);
        return rent;
    }

    @Override
    public int update(Rent rent) {
        int i =rentMapper.update(rent);

        return i;
    }

    @Override
    public boolean delete(Integer rId) {
        boolean flag=false;
        Rent rent1 = new Rent();
//        获取当前id所关联的三张表的信息
        Rent rent = rentMapper.selectOneByRId(rId);
        int i=rentMapper.deleteByPrimaryKey2(rId);
//        rent1.setRId(rId);
//        int i = rentMapper.deleteByPrimaryKey(rent1);
        if (i!=0){
            System.out.println("rent ================== " + rent);
            Integer integer = rent.getHouse().gethId();
            System.out.println("integer   house id= " + integer);
            House house = new House();
            house.sethId(integer);
            house.sethStatus(1);
            int i1 = houseMapper.updateByPrimaryKeySelective(house);
            if (i1!=0){
             flag=true;
            }

        }

        return flag;
    }

    @Override
    public List<House> getHouse() {
        Example example = new Example(House.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("hStatus",1);
        List<House> lists = houseMapper.selectByExample(example);
        return lists;
    }

    @Override
    public List<Lessee> getLessee() {
        List<Lessee> list = lesseeMapper.selectAll();
        return list;
    }

    @Override
    public int insertRent(Rent rent) {
        int a=0;
        int i = rentMapper.insertRent(rent);
        if (i!=0){
            Integer hId = rent.getHId();
            House house = new House();
            house.sethId(hId);
            house.sethStatus(0);
            int i1 = houseMapper.updateByPrimaryKeySelective(house);
            if (i1!=0){
                a=1;
            }

        }
        return a;
    }

    @Override
    public PageInfo<Rent> getRentLikeAll(String like,Integer PageNo,Integer PageSize) {
        List<Integer> list1=new ArrayList<>();

        List<Rent> list3=null;
        PageInfo<Rent> pageInfo=null;
//假设是用户名或者是用户手机号,先查询出此人的lid,再根据lid擦寻List <rent>
        List<Lessee> list = rentMapper.selectByExample3(like);
        System.out.println("list =================== " + list);

        if (list.size()!=0){

            for (Lessee one:list
                 ) {
                Integer lid = one.getlId();
                list1.add(lid);
            }

            //   根据用户名查询
                  PageHelper.startPage(PageNo,PageSize);
                 list3 =rentMapper.selectByExample4(list1);
                pageInfo = new PageInfo<>(list3,4);



            return pageInfo;
        }else {
            PageHelper.startPage(PageNo,PageSize);
            list3 = rentMapper.selectByExample5(like);
              pageInfo= new PageInfo<>(list3,4);
            return pageInfo;
        }

    }

}
